package View;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Control.ConnectDatabase;
import Interfaces.ModeInterface;

/**
 * 
 * @author stephenbwilliamson
 */
public class DatabaseConnection implements ModeInterface {
	private static DatabaseConnection instance = null;

	protected DatabaseConnection() {
		// Exists only to defeat instantiation.
	}

	public static DatabaseConnection getInstance() {
		if (instance == null) {
			instance = new DatabaseConnection();
		}
		return instance;
	}

	public JLabel jlServerURL = new JLabel("Server URL:", JLabel.RIGHT);
	public JLabel jlDatabase = new JLabel("Database Name:", JLabel.RIGHT);
	public JLabel jlUsername = new JLabel("Username:", JLabel.RIGHT);
	public JLabel jlPassword = new JLabel("Password:", JLabel.RIGHT);

	public JTextField jtServerURL = new JTextField(20);
	public JTextField jtDatabase = new JTextField(20);
	public JTextField jtUsername = new JTextField(20);
	public JPasswordField jpPassword = new JPasswordField(20);

	public JLabel jlError = new JLabel("", JLabel.RIGHT);
	public JButton jbSubmit = new JButton("Connect to Database");

	private JFrame frame;

	@Override
	public void load() {
		jtServerURL.setName("server");
		jtDatabase.setName("database");
		jtUsername.setName("username");
		jpPassword.setName("password");

		jlServerURL.setLabelFor(jtServerURL);
		jlDatabase.setLabelFor(jtDatabase);
		jlUsername.setLabelFor(jtUsername);
		jlPassword.setLabelFor(jpPassword);

		jbSubmit.addActionListener(new ConnectDatabase());

		frame = new JFrame("Enter Database Connection Properties");
		frame.setLayout(new GridLayout(5, 2, 5, 5));
		frame.add(jlServerURL);
		frame.add(jtServerURL);
		frame.add(jlDatabase);
		frame.add(jtDatabase);
		frame.add(jlUsername);
		frame.add(jtUsername);
		frame.add(jlPassword);
		frame.add(jpPassword);

		frame.add(jlError);
		frame.add(jbSubmit);

		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	public void unload() {
		frame.dispose();
		frame = null;
		instance = null;
	}
}
